(function(app){
	app
		.controller("charge_refund_PreviewCtrl",["$scope","$rootScope","myFactory",'$http','$stateParams','$state','$ionicHistory','$timeout',function($scope,$rootScope,myFactory,$http,$stateParams,$state,$ionicHistory,$timeout){
			//参数接收、定义
			var itemId =$stateParams.itemId;
			var chdrnum01=$stateParams.chdrnum01;
			var chdrcoy = $stateParams.chdrcoy;
			var pageData = $stateParams.pageData;
			var pageData2 = $stateParams.pageData2;
			var itemCommonList = $stateParams.itemCommonList;
			var strItemIds = $stateParams.strItemIds;
			//S2018122906735移动保全新增变更项目及功能的优化需求 add by mqz 20200901 start
			var isSamePerson = $stateParams.isSamePerson;
			//S2018122906735移动保全新增变更项目及功能的优化需求 add by mqz 20200901 end
			$scope.chdrnum01=$stateParams.chdrnum01;
			$scope.pageData = $stateParams.pageData;
			$scope.pageData2 = $stateParams.pageData2;
			$scope.itemList = pageData.itemList;
			$scope.fmtShowTitle = pageData.fmtShowTitle;
			$scope.fmtShowContent = pageData.fmtShowContent;
			$scope.fmtFactFeeTotal = pageData.fmtFactFeeTotal;
			$scope.hbFlag = false;//默认以平常方式统计金额
			$scope.yjFlag = false;//收费时才显示溢缴保费金额信息，平时默认不显示
			$scope.divAccountFlag = true;//用于显示选择退款授权模块，当为false时候表示隐藏，默认显示
			var token=sessionStorage.getItem("token");
			
			//H520180330_CHARGE55_BUG 变更项55犹豫期退保金额不能明文显示问题修复 add by rqyang 20180330 begin
			/******************初始操作逻辑化操作逻辑begin**************/
			len = $scope.itemList.length;
			for(var i=0;i < len ; i ++){ //如果提交的修改里面包含itemId="55"，那么最后应退金额显示为“以保险合同约定为准”,否则正常显示统计金额
				if($scope.itemList[i].itemId=="55"){
					$scope.hbFlag = true;
				}
			}
			//H520180330_CHARGE55_BUG 变更项55犹豫期退保金额不能明文显示问题修复 add by rqyang 20180330 end
			
			if(pageData.charge.type=="R"){ //收费时才显示溢缴保费金额信息
				$scope.yjFlag = true;
				//追加改动，如果溢缴费用是0，则还是不显示
				if($scope.pageData.fmtSpillPayFee == 0){
					$scope.yjFlag = false;
				}
			}
			
			//如果实际缴费金额为0则不显示账号信息
	  		var _receipt = pageData.charge.receiptAmount;	//	实际金额
	  		//S2018122906735移动保全新增变更项目及功能的优化需求 add by yangyan 20200428 start
	  		//被保险人职业、保险计划、减额缴清、补充告知  结案类型仅受理，转人工 需要进入收付费页面
	  		if( _receipt == 0 && !(strItemIds.indexOf("15")!='-1' || strItemIds.indexOf("30")!='-1' || strItemIds.indexOf("29")!='-1')){
	  		//S2018122906735移动保全新增变更项目及功能的优化需求 add by yangyan 20200428 end
//	  			divAccount.style.display = "none" ;
	  			$scope.divAccountFlag = false;//不显示
	  		}
			
			//填充数据
			var accountFlag = pageData2.accountFlag;
			if(accountFlag=="1"){//如果是选择的是续期账户
				$scope.typeName = "续期账户";  //类型
				$scope.paynme = pageData2.paynme1; //名字
				$scope.bankdesc = pageData2.bankdesc1; //银行
				$scope.citydesc = pageData2.citydesc1;//开户城市
				$scope.bankacctkey = pageData2.bankacctkey1;//账号
			}else if(accountFlag=="3"){//如果选择的是首期缴费账户
				$scope.typeName = "首期缴费账户";
				$scope.paynme = pageData2.paynme3;
				$scope.bankdesc = pageData2.bankdesc3;
				$scope.citydesc = pageData2.citydesc3;
				$scope.bankacctkey = pageData2.bankacctkey3;//账号
			}else if(accountFlag=="2"){//如果选择的是其他账户
				$scope.typeName = "其他账户";
				$scope.paynme = pageData2.paynme;
				$scope.bankdesc = pageData2.bankdesc;
				$scope.citydesc = pageData2.citydesc;
				$scope.bankacctkey = pageData2.bankacctkey;//账号
			}
			
			//收退费显示行
			if($scope.fmtShowContent == "实际应缴金额（元）"){
				$scope.fmtShowContent = "本次变更应缴金额（元）";
			}else if($scope.fmtShowContent == "实际应退金额（元）"){
				$scope.fmtShowContent = "本次变更应退金额（元）";
			}
			if(itemId == "21"){
				//如果当前变更项是部分提取，那么金额提示预计退费金额
				console.log($scope.fmtShowContent);
				$scope.fmtShowContent = "本次变更预计退费金额（元）";
			}
			
			//S2019060408807保全退保项目删除预估应退金额需求 add by yangyan 20200213 start
			$scope.payFeeShowFlag = true;
			$scope.refundedShowFlag = false;
			if(itemId == "27"){
				if ('Y' == $scope.pageData.refundedFlag) {
					$scope.fmtShowContent = "预估应退金额（元）";
					$scope.refundedShowFlag = false;
					$scope.payFeeShowFlag = false;
				}
			}
			// S2018122906735移动保全新增变更项目及功能的优化需求-保险计划 add by csf 20200813 start -->
			if (len == 1 && $scope.itemList[0].itemId=="15") {
	  			$scope.payFeeShowFlag = false;
	  		}
			// S2018122906735移动保全新增变更项目及功能的优化需求-保险计划 add by csf 20200813 end
			//S2018122906735移动保全新增变更项目及功能的优化需求 add by mqz 20200901 start
			$scope.itemList.forEach( item =>{
    			if(item.itemId='30'){
    	  			$scope.payFeeShowFlag = false;
    			}
    			if(item.itemId='29'){
    	  			$scope.payFeeShowFlag = false;
    			}
    		})
			//S2018122906735移动保全新增变更项目及功能的优化需求 add by mqz 20200901 end
			
			//S2019060408807保全退保项目删除预估应退金额需求 add by yangyan 20200213 end
			
			/******************初始操作逻辑化操作逻辑end**************/
			
			//重新受理
			$scope.renew = function(){
				myFactory.renew();
			}
			//退出移动保全
			$scope.exit = function(){
				myFactory.exit();
			}
			//保单详情弹框
			$scope.showPolicyDetail = function(){
				modal('open','policyModelOnChargeRefundPre','overwindowOnChargeRefundPre','policyCloseXOnChargeRefundPre');
				var timestamp=myFactory.getTimetamp();
				var sfis= "chdrnum-chdrcoy-token-timestamp";
				var signStr="chdrnum="+chdrnum01+"&chdrcoy="+chdrcoy+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				//初始化时候查询保单详情
				$http({
					method: 'GET',
					url: app.domain + 'query/restFindPolicyDetail.action',
					params: {'chdrnum': chdrnum01,'chdrcoy': chdrcoy,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						var policyData = response.data.data;
						$scope.policyData = policyData;
						$scope.policyData.ptdate01=myFactory.DateTools($scope.policyData.ptdate01);
					}
				}, function errorCallback(response) {
					myFactory.alert("","初始化查询订单详情失败!","info");
				});
			}
			//保单详情弹窗关闭函数
			$scope.policyCancelOn27Input = function(){
				modal('close','policyModelOnChargeRefundPre','overwindowOnChargeRefundPre','policyCloseXOnChargeRefundPre');
			}
			//关闭遮罩
			$scope.overwindowClose = function(){
				//$(".overwindow").show();
				$(".overwindow").hide();
				$(".overmodal").hide();
			}
			//返回上一步
			$scope.goBack = function () {
				/**
				 * S2018062604412网联政策630实施相关系统功能改造需求（移动保全2.0增加签约功能）
				 * 新增签约接口的逻辑 add by csf 20180926 start
				 */
				if( $rootScope['flag']  =='2' ){ 
					$rootScope['flag'] ='';
					$ionicHistory.goBack(-2);
				} else{
					myFactory.goBack();
				}
				/**
				 * S2018062604412网联政策630实施相关系统功能改造需求（移动保全2.0增加签约功能）
				 * 新增签约接口的逻辑 add by csf 20180926 end
				 */
				//myFactory.goBack();
			}
			//下一步
			$scope.nextStep = function(){
				myFactory.loadingShow();
				var timestamp=myFactory.getTimetamp();
				var sfis= "itemId-token-timestamp";
				var signStr="itemId="+itemId+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				//数据已经从上一个页面得到，这边直接提交后台，成功的话跳转即可
				$http({
					method: 'GET',
					url: app.domain + 'charge/restSave.action',
					params: {'itemId': itemId,'chargeStr': pageData2,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						var DataSave = response.data.data.pageData;
						var nextStep= DataSave.nextStep;//"charge/restInit.action";
						var message = DataSave.message;//用于检测实时缴费是否成功
						
						sfis= "itemId-message-token-timestamp";
						signStr="itemId="+itemId+"&message="+message+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
						sign = myFactory.hex(signStr);
						//提交后台保存后，再次跳转到restInit.action，这次附带上检测标志的message
						$http({
							method: 'GET',
							url: app.domain + nextStep,
							params: {"itemId": itemId,"message":message,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
						}).then(function successCallback(response) {
							if (response.data.success) {
								var Data = response.data.data.pageData;
								var charge = Data.charge;
								var intime = Data.intime;
								var message = Data.message;
								var type = charge.type;
								
								//以下这段逻辑跟check_mainCtrl中相似，一般来说，这里返回回来一定会得到message的结果，然后直接跳签名
								if(message!=undefined && message!=null && message!="" && message=="success" ){
									//如果是success这种情况，表示保存成功，这里直接跳转签名
//									myFactory.alert("","message为success时，这里要跳到签名，暂时先跳到end_main","info");
//									$state.go('end_main',{
//										itemId:itemId,
//										chdrnum01:chdrnum01,
//										chdrcoy:chdrcoy,
//										itemCommonList:itemCommonList,
//									    strItemIds:strItemIds
//									});
									sfis= "itemId-token-timestamp";
									signStr="itemId="+itemId+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
									sign = myFactory.hex(signStr);
									/*签名留影*/
									$http({
										method: 'GET',
										url: app.domain + "img/gotoSignMain.action",
										params: {"itemId": itemId,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
									}).then(function successCallback(response) {
										if (response.data.success) {
											myFactory.loadingHide();
											var signMainData = response.data.data.signMainData;
											if(signMainData!=undefined && signMainData!=null && signMainData!=""){
												$state.go('photo_sign',{
													itemId:itemId,
													chdrnum01:chdrnum01,
													chdrcoy:chdrcoy,
													//S2018122906735移动保全新增变更项目及功能的优化需求 add by mqz 20200901 start
													signMainData:signMainData,
						                            isSamePerson: isSamePerson
						                			//S2018122906735移动保全新增变更项目及功能的优化需求 add by mqz 20200901 end
												});
											}else{
												//.............
											}
										}
									}, function errorCallback(response) {
										myFactory.loadingHide();
										myFactory.alert("","访问后台出错，请稍后再试!","info");
									});
								}else{
									if(intime!=undefined && intime!=null && intime!=""){
										//这里进入实时缴费界面
										//myFactory.alert("","这里进入实时缴费界面","info");
									}else{//非实时缴费则走下面
										if(type=="R"){//收费，跳到页面charge_fee
											//myFactory.alert("","这里之后要跳到收费界面","info");
										}else if(type=="P"){//退费，跳到页面charge_refund
											myFactory.loadingHide();
											$ionicHistory.clearCache().then(function(){
												$state.go("charge_refund",{
													itemId:itemId,
													chdrnum01:chdrnum01,
													chdrcoy:chdrcoy,
													pageData:Data,
													itemCommonList : itemCommonList,
												    strItemIds : strItemIds
												});
											})
										}else{//其他情况，如""，要跳签名action，一般到收付费界面不可能走这个分支，走了可能说明系统出错，提示重来
											myFactory.loadingHide();
											myFactory.alert("","数据提交后台时候出错，请返回重新尝试!","info");
										}
									}
								}
							}else{
								myFactory.loadingHide();
							}
						}, function errorCallback(response) {
							myFactory.loadingHide();
							myFactory.alert("","数据提交后台时候出错，请返回重新尝试!","info");
						});
						
					}
				}, function errorCallback(response) {
					myFactory.loadingHide();
					myFactory.alert("","数据提交后台时候出错，请返回重新尝试!","info");
				});
			}
			
			
		}]);
	
		//保单详情弹窗
		app.directive('infoid',function(){
			return {
				restrict: 'EA',		//指明指令在DOM里面以什么形式被声明,EA.表示即可以是元素也可以是属性
				templateUrl: 'html/input/content_base_info.html'+app.clearCache,
				replace: true,		//设置解析后，DOM声明的指令是否消失
				link: function(scope,element,attrs){  //链接函数负责注册DOM事件和更新DOM,
					console.log('弹框指令完成');
				}
			}
		});
})(app)